package com.jp.knowledge.adapter.team_circle;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.SearchResultModel;

import java.util.List;


public class TeamCircleCompanySearchListAdapter extends RecycleVAdapter<SearchResultModel> implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private boolean showCheckBox;
    public TeamCircleCompanySearchListAdapter(Context context, @NonNull List<SearchResultModel> list) {
        super(context, list);
        showCheckBox = true;
    }

    public void setShowCheckBox(boolean isShow){
        this.showCheckBox = isShow;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_search_data;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        SearchResultModel model = getData(position);
        holder.setTextView(R.id.item_data_tv_name, model.getName());
        holder.setImageUrl(R.id.item_data_iv_logo,
                model.getIcon() + "?imageView2/2/w/80/interlace/1");
        holder.getConvertView().setTag(position);
        holder.getConvertView().setOnClickListener(this);

        CheckBox itemSelectCb = holder.get(R.id.item_data_cb);
        itemSelectCb.setOnCheckedChangeListener(this);
        itemSelectCb.setTag(position);
        itemSelectCb.setChecked(model.getIsSelect());
        if(showCheckBox)
            itemSelectCb.setVisibility(View.VISIBLE);
        else
            itemSelectCb.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        CheckBox checkBox = (CheckBox) v.findViewById(R.id.item_data_cb);
        checkBox.setChecked(!checkBox.isChecked());
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(mList.get((int) buttonView.getTag()).getIsSelect() == isChecked)
            return;
        if(this.onSearchItemSelectedListener != null)
            this.onSearchItemSelectedListener.onItemSelect((int) buttonView.getTag(), isChecked);
    }

    private OnSearchItemSelectedListener onSearchItemSelectedListener;
    public void setOnSearchItemSelectedListener(OnSearchItemSelectedListener onSearchItemSelectedListener){
        this.onSearchItemSelectedListener = onSearchItemSelectedListener;
    }
    public interface OnSearchItemSelectedListener{
        void onItemSelect(int position, boolean isChecked);
    }
}
